package party.loveit.iflytek.core;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.SpeechUtility;

import java.util.List;
import java.util.Vector;

import party.loveit.iflytek.interfaces.SpeechResultListener;

/**
 * Created by MRKING on 2017/2/16.
 */

public class SpeechResult {
    private static SpeechResult instance;
    private static SpeechRecognizer mIat;
    private static StringBuffer mResult = new StringBuffer();
    private static RecognizerListener mRecognizerListener;
    private static final List<SpeechResultListener> mListeners = new Vector<>();

    private SpeechResult() {
    }

    public static void init(Context context, boolean isMainProcess) {
        if (instance == null) {
            instance = new SpeechResult();
            if(isMainProcess)
              SpeechUtility.createUtility(context, SpeechConstant.APPID + "=58a3d0e6");
            else
                SpeechUtility.createUtility(context, SpeechConstant.APPID + "=58a3d0e6," +  SpeechConstant.FORCE_LOGIN +"=true");
            //1.创建SpeechRecognizer对象，第二个参数：本地听写时传InitListener
            mIat = SpeechRecognizer.createRecognizer(context, null);
//2.设置听写参数，详见《科大讯飞MSC API手册(Android)》SpeechConstant类
            mIat.setParameter(SpeechConstant.DOMAIN, "iat");
            mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
            mIat.setParameter(SpeechConstant.ACCENT, "mandarin ");
            mRecognizerListener = new Recognizer();
        }
    }


    public static synchronized SpeechResult getInstance() {
        if (instance == null) {
            throw new RuntimeException("you must call init(Context context) method first");
        }
        return instance;
    }

    public void startSpeech() {
        if (mIat != null && mRecognizerListener != null) {
            if (mResult.length() > 0)
                mResult.delete(0, mResult.length());
            mIat.startListening(mRecognizerListener);
        }
    }

    public void register(SpeechResultListener listener) {
        mListeners.add(listener);
    }

    public void unRegister(SpeechResultListener listener) {
        mListeners.remove(listener);
    }

    private static void onSeechResult(int states) {
        synchronized (mListeners) {

            for (SpeechResultListener listener : mListeners) {
                switch (states) {
                    case States.PAUSE:
                        listener.onSpeechPause();
                        break;
                    case States.PLAY:
                        listener.onSpeechPlay();
                        break;
                    case States.NEXT:
                        listener.onSpeechNext();
                        break;
                    case States.PREVIOUS:
                        listener.onSpeechPrevious();
                        break;
                    case States.POSITIVE:
                        listener.onSpeechPositive();
                        break;
                    case States.DANMU:
                        listener.onSpeechDanMu(mResult.toString());
                        break;
                }
                listener.onSpeechResult(mResult.toString());
            }
        }
    }

    private static class Recognizer implements RecognizerListener {

        @Override
        public void onVolumeChanged(int i, byte[] bytes) {

        }

        @Override
        public void onBeginOfSpeech() {

        }

        @Override
        public void onEndOfSpeech() {

        }

        @Override
        public void onResult(RecognizerResult recognizerResult, boolean isLast) {
            if (recognizerResult == null || recognizerResult.getResultString() == null)
                return;
            Log.d("Result:", recognizerResult.getResultString() + "<--isLast--->" + isLast);
            mResult.append(JSONParse.parseIatResult(recognizerResult.getResultString()));
            if (isLast) {
                Log.d("Result:", "reasult-->" + mResult.toString());
                String tempStr = mResult.toString();
                if (tempStr == null) {
                    return;
                }
                if (tempStr.contains("暂停") || tempStr.contains("停止")) {
                    onSeechResult(States.PAUSE);
                } else if (tempStr.contains("播放") || tempStr.contains("开始")) {
                    onSeechResult(States.PLAY);
                } else if (tempStr.contains("换台") || tempStr.contains("下一个频道") || tempStr.contains("下一个")) {
                    onSeechResult(States.NEXT);
                } else if (tempStr.contains("上一个频道") || tempStr.contains("上一个")) {
                    onSeechResult(States.PREVIOUS);
                } else if (tempStr.contains("回正")) {
                    onSeechResult(States.POSITIVE);
                } else {
                    onSeechResult(States.DANMU);
                }

                if (mIat != null)
                    mIat.stopListening();
            }
        }

        @Override
        public void onError(SpeechError speechError) {

        }

        @Override
        public void onEvent(int i, int i1, int i2, Bundle bundle) {

        }
    }
}
